// JavaScript Document
$(document).ready(function() {
  $('#edit-crm-created-date').datepicker({
    dateFormat:'mm-dd-yy'
  });

  $("#edit-crm-group-activate").click(function() {
    var checkbox_values = [];
    $('input[@name=checkBoxList]:checked').each(function(i){
      checkbox_values[i] = $(this).val(); //store the selected values in an array
    });
    if(checkbox_values.length == 0){
      openAlertDialog(400, 150, 'Information', 'Please select atleast one event type to activate', 'information');
      return false;
    }
    var n =0;
    for(i=0;i<checkbox_values.length;i++){
      var crmActive;
      crmActive = $('#event_type_status_'+checkbox_values[i]).html();
      n = crmActive.search('Active');
      if(n >= 0 ){
        openAlertDialog(400, 150, 'Information', 'Please select the event type which is in Inactive status', 'information');
        return false;
      }
    }
    activateDeactivateItems(checkbox_values, 'true');
    return false;
  });
  $("#edit-crm-group-deactivate").click(function() {
    var checkbox_values = [];
    $('input[@name=checkBoxList]:checked').each(function(i){
      checkbox_values[i] = $(this).val(); //store the selected values in an array
    });
    if(checkbox_values.length == 0){
      openAlertDialog(400, 150, 'Information', 'Please select atleast one event type to deactivate', 'information');
      return false;
    }
    var n =0;
    for(i=0;i<checkbox_values.length;i++){
      var crmActive;
      crmActive = $('#event_type_status_'+checkbox_values[i]).html();
      n = crmActive.search('Inactive');
      if(n >= 0 ){
        openAlertDialog(400, 150, 'Information', 'Please select the event type which is in Active status', 'information');
        return false;
      }
    }
    activateDeactivateItems(checkbox_values, 'false');
    return false;
  });

  $("#edit-more-actions").click(function(){
    moreactions();
    return false;
  });
});

function activateDeactivateItems(checkbox_values, status){
  var title;
  var msg_str;
  var question;
  var action;
  if(status=='true'){
    title       = 'Activated';
    msg_str     = 'activated';
    question    = 'activate';
    action      = 'Activate';
  }else{
    title       = 'Deactivated';
    msg_str     = 'deactivated';
    question    = 'deactivate';
    action      = 'Deactivate';
  }
  openConfirmBox(function(){
    $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
    $.ajax({
      type: "POST",
      url : baseurl+'crm/groupaction',
      data: '&event_type_ids=' + checkbox_values + '&event_type_status=' + status,
      success: function(res){
        $('#alertdialog').dialog('destroy');
        eval("var json="+res);
        if(json.Message == 'Success'){
          var pageno   = $('#hid_pageno').val();
          applyEventTypeFilterAction(pageno);
          openAlertDialog(400, 150, title, 'Selected event types '+msg_str+' successfully','success');
          $('input[name=checkAll]').attr('checked', false);
          return false;
        }
        else{
          openAlertDialog(400, 150, 'Error', 'Error in updating the event types');
          return false;
        }
      },
      error : function (res) {
        handleRequestError(res);
      }
    });
  },300,120,"Confirm "+action,(question=="deactivate"?"Event type will not appear in any of the associated sites":"Do you want to "+question+"?"));
  return false;
}


//function to close the dialog box
function closeDialog(){
  $('#dialog').dialog('destroy');
}

function gohome(){
  window.location = baseurl + 'crm/assign';
}

//function to apply filter option
function applyEventTypeFilterAction(pageno){
  //prepare the field values for filtering
  var event_name    = $.trim($("#edit-crm-eventtype").val());
  var event_status  = $.trim($("#edit-crm-status").val());
  var event_date    = $("#edit-crm-created-date").val();
  var sortval       = $('#hid_sort').val();

  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").hide();
  $("#pagingdivbottom").hide();

  $.ajax({
    type : "POST",
    url : baseurl + "crm/filter",
    data : "event_name="+encodeURIComponent(event_name)+"&event_status="+encodeURIComponent(event_status)+"&event_date="+encodeURIComponent(event_date)+(pageno>1?'&page='+pageno:'')+(sortval!=""?'&sorting='+sortval:''),
    async : false,
    cache : false,
    success : function (msg) {
      $("#listingResultset").html(msg);
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno);
      var counter = $("#hid_totalRec").val();

      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyEventTypeFilterAction('+(page-1)+')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += ' <span class="red">|</span> <a href="javascript:void(0)" onclick = "applyEventTypeFilterAction('+(page+1)+')" class="red">Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
        $("#pagingdivtop").show();
        $("#pagingdivbottom").show();
      }
    }
  });
  $("#hid_pageno").val(pageno);
  return false;
}

function filterdata_crm(){
  //prepare the field values for filtering
  var event_type    = $.trim($("#edit-crm-eventtype").val());
  var event_status  = $("#edit-crm-status").val();
  var event_date    = $("#edit-crm-created-date").val();

  var data = "";
  if($.trim(event_type)!=""){
    data+="&event_name="+event_type;
  }
  if($.trim(event_status)!=""){
    data+="&event_status="+event_status;
  }
  if($.trim(event_date)!=""){
    data+="&event_date="+event_date;
  }
  return data;
}

function openViewEventDetailDialog(id, width, height, title){
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:50px; padding-left:220px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  viewEventTypeDetail(id);
}

function viewEventTypeDetail(id){
  $.ajax({
    type  : "GET",
    url   : baseurl + 'crm/event_type_detail/' + id,
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}


//function to open dialog box for adding new event type
function addNewEventTypeDialog (width, height, title) {
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  addEventType();
}

//Function to display add event type form
function addEventType() {
  $.ajax({
    type  : "GET",
    url   : baseurl + 'crm/add_event_type',
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $("#edit-event-code").focus();
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

//Function to save the data for event type
function submitEventType() {
  var event_name    = $.trim($("#edit-event-name").val());
  var event_status  = $.trim($("#edit-event-status").val());
  var event_code    = $.trim($("#edit-event-code").val());
  var msg_str       = '';
  var lines = 0;
  if (event_code == "") {
    msg_str = msg_str + "Please enter the event type code<br />";
    lines += 25;
  }
  if (event_name == "") {
    msg_str = msg_str + "Please enter the event type name<br />";
    lines += 25;
  }
  if (event_status == "") {
    msg_str = msg_str + "Please select the event type status";
    lines += 25;
  }

  if(msg_str != ""){
    resize_dialog(600, 200+lines);
    styleStatusMessages(msg_str, "statusMessage", "failure");
    return false;
  }
  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type    : "POST",
    url     : baseurl + 'crm/add_eventtype_action',
    data    : '&event_name='+encodeURIComponent(event_name) + '&event_status=' + encodeURIComponent(event_status)+ '&event_code=' + encodeURIComponent(event_code),
    success: function(msg){
      eval("var json="+msg);
      if(json.Message=="Success"){
        $('#dialog').dialog('destroy');
        applyEventTypeFilterAction(1);
        openAlertDialog(350, 150, 'Information', 'Event type added successfully.', "success");
      }else if(json.Message=="Failure"){
          $.ajax({
            type    : "GET",
            url     : baseurl + 'crm/add_event_type',
            cache   : false,
            success : function (res) {
              $('#dialog').html(res);
              $('#edit-event-name').val(event_name);
              $("#edit-event-status").val(event_status);
              $("#edit-event-code").val(event_code);
              styleStatusMessages(json.Error, "statusMessage", "failure");
            },
            error   : function (res) {
              handleRequestError(res);
            }
          });
          resize_dialog(600, 220);
      }
    }
  });
  return false;
}

function activateEventType(id, status){
  if(status=="Deactivate"){
    openConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      confirmActivateEventType(id, status);
    },300,140,"Confirm Deactivate","Event type will not appear in any of the associated sites");
  }else{
    confirmActivateEventType(id, status);
  }
  return false;
}

function confirmActivateEventType(id, status){
  $('#dialog').html('<div style="padding-top:60px; padding-left:220px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." title="Loading... Please Wait !!!" border="0"></div>');
  var pageno = $("#hid_pageno").val();
  var message_str;
  if(status=="Deactivate"){
    status = "false";
    message_str = 'deactivated';
  }
  else {
    status = "true";
    message_str = 'activated';
  }

  $.ajax({
    type : "POST",
    url : baseurl+"crm/edit_eventtype_action/activate",
    data: "status="+status+"&eventTypeId="+id,
    async : false,
    cache : false,
    success : function (msg) {
      eval("var json="+msg);
      if(json.Message=="Success"){
        $('#dialog').dialog('destroy');
        applyEventTypeFilterAction(pageno);
        openAlertDialog(350, 150, 'Information', 'Event type '+message_str+' successfully.', "success");
      }else{
        openAlertDialog(350, 150, 'Information', 'Error in service');
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function editEventTypeDialog(id, width, height, title) {
  $('#dialog').dialog('destroy');
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:10px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  editEventType(id, title);
}

function editEventType(id, title){
  reopen_dialog(600,200,"Edit " + title);
  $.ajax({
    type : "GET",
    url : baseurl+'crm/edit_event_type/'+id,
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $("#edit-event-name").focus();
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function editEventTypeAction(eventTypeId) {
  var event_code    = $.trim($("#edit-event-code").val());
  var event_name    = $.trim($("#edit-event-name").val());
  var event_status  = $.trim($("#edit-event-status").val());
  var msg_str       = '';
  var height        = 0;

  if (event_code == "") {
    msg_str = msg_str + "Please enter the event type code. <br />";
    height = height + 20;
  }
  if (event_name == "") {
    msg_str = msg_str + "Please enter the event type name. <br />";
    height = height + 20;
  }
  if (event_status == "") {
    msg_str = msg_str + "Please select the event type status. <br />";
    height = height + 20;
  }
  if(msg_str != ""){
    resize_dialog(600, 200+height);
    styleStatusMessages(msg_str, "statusMessage", "failure");
    return false;
  }
  if(event_status=="Active"){
    event_status = "true";
  }
  else {
    event_status = "false";
  }
  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type    : "POST",
    url     : baseurl + 'crm/edit_eventtype_action',
    data    : '&eventTypeId='+encodeURIComponent(eventTypeId)+ '&event_name='+encodeURIComponent(event_name)+ '&event_code='+encodeURIComponent(event_code)+ '&event_status='+encodeURIComponent(event_status),
    success: function(msg){
      eval("var json="+msg);
      if(json.Message=="Success"){
        $('#dialog').dialog('destroy');
        var pageno = parseInt($('#hid_pageno').val());
        applyEventTypeFilterAction(pageno);
        openAlertDialog(350, 150, 'Information', 'Event type updated successfully.', "success");
      }else if(json.Message=="Failure"){
          $.ajax({
            type : "GET",
            url : baseurl+'crm/edit_event_type/'+eventTypeId,
            async : true,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
              $('#edit-event-name').val(event_name);
              resize_dialog(600, 220);
              styleStatusMessages(json.Error, "statusMessage", "failure");
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
      }
    }
  });
  return false;
}

function moveOptions(elem1id, elem2id){
  var count       = 0;
  var elem1Length = elem1id.options.length;
  for(i=elem1id.options.length-1; i>=0; i--) {
    if(elem1id[i].selected){
      addOption(elem2id, elem1id[i].text, elem1id[i].value);
      removeOption(elem1id, i);
      count++;
    }
  }
  if(elem1Length == 0){
    openAlertDialog(350, 150, 'Information', 'There are no event types to select.', 'information');
  }else if(count == 0){
    openAlertDialog(350, 150, 'Information', 'Please select atleast one event type.', 'information');
  }
}

function addOption(selectbox, text, value ){
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  selectbox.options.add(optn);
}

function removeOption(listbox,i){
  listbox.remove(i);
}

function moveAllOptions(elem1id, elem2id){
  var elem1Length = elem1id.options.length;
  if(elem1Length == 0){
    openAlertDialog(350, 150, 'Information', 'There are no event types to select.', 'information');
  }else{
    for(i=elem1id.options.length-1; i>=0; i--) {
      addOption(elem2id, elem1id[i].text, elem1id[i].value);
      removeOption(elem1id, i);
    }
  }
}

function associationAction(){
  var associatedOptionsArray = new Array();
  var elem1id = document.getElementById('edit-associated-eventtypes');
  for(i=0; i<=elem1id.options.length-1; i++){
    associatedOptionsArray[i] = elem1id[i].value;
  }
  var associatedOptions = associatedOptionsArray.join(',');
    
  $.ajax({
    type    : "POST",
    url     : baseurl + 'crm/assign_action',
    data    : '&eventTypeIds='+encodeURIComponent(associatedOptions),
    success: function(msg){
      eval("var json="+msg);
      if(json.Message=="Success"){
        openAlertDialog(350, 150, 'Information', 'Event type updated successfully.', "success");
      }else{
        if(json.Message=="Failure"){
          openAlertDialog(350, 150, 'Information', json.Error);
        }else{
            openAlertDialog(350, 150, 'Information', 'There is some error in service.');
        }
      }
    }
  });
  return false;
}

function authorizationKeyDialog(width, height, title){
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:50px; padding-left:250px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  authorizationKeyManagement();
}

function authorizationKeyManagement(){
  $.ajax({
    type  : "GET",
    url   : baseurl + 'crm/manage_authorization',
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#edit-authorization-key').focus();
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}

function authorizationAction(action){
  var auth_id     = $.trim($('#edit-authorization-key-id').val());
  var auth_status = $('#edit-authorization-key-status').val();
  var auth_access = $('#edit-authorization-key-access').val();
  var msg_str     = '';
  var height      = 0;

  if(auth_status == ''){
    msg_str = msg_str + 'Please select the status <br />';
    height  = height + 20;
  }
  if(auth_access == ''){
    msg_str = msg_str + 'Please select the access level <br />';
    height  = height + 20;
  }

  if(msg_str != ''){
    resize_dialog(600, 200 + height);
    styleStatusMessages(msg_str, "statusMessage", "failure");
    return false;
  }
  $.ajax({
    type    : "POST",
    url     : baseurl + 'crm/manage_authorization_action',
    data    : '&auth_id='+auth_id+'&auth_status='+encodeURIComponent(auth_status)+'&auth_access='+encodeURIComponent(auth_access),
    success: function(msg){
      eval("var json="+msg);
      if(json.Message=="Success"){
        $('#dialog').dialog('destroy');
        openAlertDialog(350, 150, 'Information', 'Authorization key data '+(action=="edit"?"updated":"created")+' successfully.', "success");
      }else{
        if(json.Message=="Failure"){
          $.ajax({
            type : "GET",
            url : baseurl+'crm/manage_authorization',
            async : true,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
              $('#edit-authorization-key-id').val(auth_id);
              $('#edit-authorization-key-status').val(auth_status);
              $('#edit-authorization-key-access').val(auth_access);
              resize_dialog(600, 230);
              styleStatusMessages(json.Error, "statusMessage", "failure");
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
        }
      }
    }
  });
  return false;
}

/**
 * List all authorization key associated to the site
 */
function viewauthorizationKeyDialog(width, height, title){
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:50px; padding-left:250px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  viewauthorizationKey();
}

function viewauthorizationKey(){
  $.ajax({
    type  : "GET",
    url   : baseurl + 'crm/view_authorization',
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}

function manageSiteDialog(width, height, title){
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:50px; padding-left:250px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  manageSite();
}
function manageSite(){
  $.ajax({
    type  : "GET",
    url   : baseurl + 'crm/manage_site',
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}
function save_managesite(action){
  var site_code = $.trim($('#edit-crm-sitecode').val());
  if(site_code == ''){
    resize_dialog(550, 220);
    styleStatusMessages("Please enter site code", "statusMessage", "failure");
    return false;
  }
  $.ajax({
    type  : "POST",
    url   : baseurl + 'crm/manage_site_action'+(action=="edit"?"/edit":""),
    data  : "site_code="+encodeURIComponent(site_code),
    async : true,
    cache : false,
    success : function (res) {
      eval("var msg="+res);
      if(msg.Message=="Success"){
        $('#dialog').dialog('destroy');
        openAlertDialog(300, 130, 'Information', 'Site code saved successfully','success');
        refreshParentWindow();
      }else{
        if(msg.Error=="Event type already exists"){
          resize_dialog(550, 220);
          styleStatusMessages("Site code already exists", "statusMessage", "failure");
        }else{
          $('#dialog').dialog('destroy');
          openAlertDialog(300, 130, 'Error', 'Error in site code save');
        }
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}

function refreshParentWindow(){
    window.location = baseurl + 'crm/assign';
}

/**
 * Edit authorization key associated to the site
 */
function editauthorizationKeyDialog(keyid, width, height, title){
  var wid       = width;
  var heit      = height;
  var buttons   = false;

  $('#dialog').dialog({
    autoOpen    : false,
    width       : wid,
    height      : heit,
    bgiframe    : true,
    resizable   : false,
    title       : title,
    modal       : true,
    show        : 'puff',
    close       : function () {
      $('#dialog').dialog('destroy');
    }
  });
  $('#dialog').dialog('open');
  var h         = heit;
  if(buttons){
    h           = heit-95;
  }else{
    h           = heit-35;
  }
  $('#dialog').css({
    'overflow-y': 'auto',
    'width'     : (wid-18)+'px',
    'height'    : h + 'px'
  }).show();

  $('#dialog').html('<div style="padding-top:50px; padding-left:250px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  editauthorizationKey(keyid);
}

function editauthorizationKey(keyid) {  
  $.ajax({
    type  : "GET",
    url   : baseurl + 'crm/manage_authorization/' + keyid,
    async : true,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}